<?php

/*
	info: 登录
*/

require dirname(__FILE__).'/include/common.inc.php';

if ($webglobal['action']!='exit' && $user['islogin'])
{
	$tempgourl = (!empty($webserver['comeurl']) && !preg_match('/^.*login.*$/', $webserver['comeurl'])) ? $webserver['comeurl'] : WEBURLPATH.'/admin.php';
	global_ShowMessage("欢迎回来。", $tempgourl);
}

//退出登录
if ($webglobal['action']=='exit')
{
	$objuser->ClearUserCookie();
	global_ShowMessage('您已成功退出。', WEBURLPATH.'/login.php');
}
//检测登陆
elseif ($webglobal['action']=='login')
{
	global_CheckPost(
		array(
			array('username','str','','请输入用户名',1),
			array('password','str','','请输入密码',1),
			array('keeplogin','bool','','',0,0),
	));
	$webglobal['CPP']['username'] = isset($webglobal['CPP']['username']) ? stripslashes($webglobal['CPP']['username']) : '';
	if (empty($webglobal['CPM']))
	{
		$tempstatus = $objuser->CheckUser($webglobal['CPP']['username'], $webglobal['CPP']['password']);
		if ($tempstatus)
		{
			$setcookietime = $webglobal['CPP']['keeplogin'] ? 1209600 : 0;
			$objuser->SetUserCookie($webglobal['CPP']['username'], md5($webglobal['CPP']['password']), $setcookietime);
			global_GoToUrl(WEBURLPATH.'/admin.php');
		}
		else
		{
			$webglobal['CPM'][] = $objuser->err;
		}
	}
	$webglobal['loginusername'] = $webglobal['CPP']['username'];
	$webglobal['loginerrinfo'] = !empty($webglobal['CPM']) ? reset($webglobal['CPM']) : '';
}
//用户登录
else
{
	$webglobal['loginusername'] = $webglobal['loginerrinfo'] = '';
}

template_Display('login');
